//
//  HNSH_HomeRecommendCell2.m
//  HNSH_SingleProject
//
//  Created by 李宇廷 on 2023/5/25.
//

#import "HNSH_HomeRecommendCell2.h"
#import "HNSH_GoodsDetailViewController.h"

@interface HNSH_HomeRecommendCellItem2 : UIView

/* imgV */
@property (nonatomic, strong) UIImageView *imgV;
/* moneyLabel */
@property (nonatomic, strong) UILabel *moneyLabel;

/* clickBlock */
@property (nonatomic, copy) dispatch_block_t clickBlock;

@end

@implementation HNSH_HomeRecommendCellItem2

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addAllViews];
        [self addAllLayout];
    }
    return self;
}

- (void)addAllViews {
    [self addSubview:self.imgV];
    [self addSubview:self.moneyLabel];
    
    self.userInteractionEnabled = YES;
    __weak __typeof(&*self)weakSelf = self;
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
        if (weakSelf.clickBlock) {
            weakSelf.clickBlock();
        }
    }];
    [self addGestureRecognizer:tap];
}

- (void)addAllLayout {
    [self.imgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.width.height.mas_equalTo(74);
        make.centerX.equalTo(self.mas_centerX);
    }];
    [self.moneyLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(0);
        make.top.equalTo(self.imgV.mas_bottom).offset(6);
        make.height.mas_equalTo(16);
    }];
}

- (UIImageView *)imgV{
    if (!_imgV) {
        _imgV = [UIImageView new];
        _imgV.contentMode = UIViewContentModeScaleAspectFill;
    }
    return _imgV;
}

- (UILabel *)moneyLabel {
    if (!_moneyLabel) {
        _moneyLabel = [UILabel new];
        _moneyLabel.textColor = [UIColor appGreenColor];
        _moneyLabel.font = [UIFont boldSystemFontOfSize:11];
        _moneyLabel.textAlignment = NSTextAlignmentCenter;
        _moneyLabel.backgroundColor = [UIColor colorWithHexString:@"#FFECEC"];
        _moneyLabel.layer.cornerRadius = 8.0f;
        _moneyLabel.layer.masksToBounds = YES;
    }
    return _moneyLabel;
}

@end



@interface HNSH_HomeRecommendCell2 ()

/* leftImgV */
@property (nonatomic, strong) UIImageView *leftImgV;
/* leftTitleLabel */
@property (nonatomic, strong) UILabel *leftTitleLabel;

/* rightImgV */
@property (nonatomic, strong) UIImageView *rightImgV;
/* rightTitleLabel */
@property (nonatomic, strong) UILabel *rightTitleLabel;

@end

@implementation HNSH_HomeRecommendCell2

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        self.contentView.backgroundColor = [UIColor clearColor];
        [self configUI];
    }
    return self;
}

- (void)setModel:(HNSH_HomeViewModel *)model {
    _model = model;
    [self.leftImgV.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    [self.rightImgV.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    CGFloat spece = ((kScreenWidth-24)/2-12-74*2)/2;
    for (int i = 0; i < (model.czg_list.count > 2 ? 2 : model.czg_list.count); i ++) {
        HNSH_GoodsListModel *goodsModel = model.czg_list[i];
        HNSH_HomeRecommendCellItem2 *goods = [[HNSH_HomeRecommendCellItem2 alloc]initWithFrame:CGRectMake(12+(74+spece)*i, 34, 74, 128)];
        [goods.imgV sd_setImageWithURL:[NSURL URLWithString:goodsModel.cover_url]];
        goods.moneyLabel.text = [NSString stringWithFormat:@"  ¥%.2f  ",[goodsModel.price doubleValue]];
        [self.leftImgV addSubview:goods];
        goods.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
            vc.goodsId = goodsModel.uid;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
        [goods addGestureRecognizer:tap];
    }
    
    for (int i = 0; i < (model.kbhp_list.count > 2 ? 2 : model.kbhp_list.count); i ++) {
        HNSH_GoodsListModel *goodsModel = model.kbhp_list[i];
        HNSH_HomeRecommendCellItem2 *goods = [[HNSH_HomeRecommendCellItem2 alloc]initWithFrame:CGRectMake(12+(74+spece)*i, 34, 74, 128)];
        [goods.imgV sd_setImageWithURL:[NSURL URLWithString:goodsModel.cover_url]];
        goods.moneyLabel.text = [NSString stringWithFormat:@"  ¥%.2f  ",[goodsModel.price doubleValue]];
        [self.rightImgV addSubview:goods];
        goods.userInteractionEnabled = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithActionBlock:^(id  _Nonnull sender) {
            HNSH_GoodsDetailViewController *vc = [[HNSH_GoodsDetailViewController alloc]init];
            vc.goodsId = goodsModel.uid;
            [[UIViewControllerHelper findCurrentShowingViewController].navigationController pushViewController:vc animated:YES];
        }];
        [goods addGestureRecognizer:tap];
    }
}

- (void)configUI {
    [self.contentView addSubview:self.leftImgV];
    [self.contentView addSubview:self.leftTitleLabel];
    [self.contentView addSubview:self.rightImgV];
    [self.contentView addSubview:self.rightTitleLabel];
    
    
    CGFloat width = (kScreenWidth-32)/2;
    [self.leftImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(12);
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(width);
    }];
    [self.leftTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(20);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
    }];
    
    [self.rightImgV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo((kScreenWidth/2+4));
        make.top.bottom.mas_equalTo(0);
        make.width.mas_equalTo(width);
    }];
    [self.rightTitleLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(8);
        make.left.mas_equalTo(kScreenWidth/2+12);
        make.width.mas_equalTo(70);
        make.height.mas_equalTo(20);
    }];
    
}

#pragma mark lazy
- (UIImageView *)leftImgV {
    if (!_leftImgV) {
        _leftImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_recommend_bg_2"]];
        _leftImgV.userInteractionEnabled = YES;
    }
    return _leftImgV;
}

- (UILabel *)leftTitleLabel {
    if (!_leftTitleLabel) {
        _leftTitleLabel = [UILabel new];
        _leftTitleLabel.textColor = [UIColor appBlackLabelColor];
        _leftTitleLabel.font = [UIFont boldSystemFontOfSize:14];
        _leftTitleLabel.text = @"超值购";
    }
    return _leftTitleLabel;
}

- (UIImageView *)rightImgV {
    if (!_rightImgV) {
        _rightImgV = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"home_recommend_bg_3"]];
        _rightImgV.userInteractionEnabled = YES;
    }
    return _rightImgV;
}

- (UILabel *)rightTitleLabel {
    if (!_rightTitleLabel) {
        _rightTitleLabel = [UILabel new];
        _rightTitleLabel.textColor = [UIColor appBlackLabelColor];
        _rightTitleLabel.font = [UIFont boldSystemFontOfSize:14];
        _rightTitleLabel.text = @"口碑好品";
    }
    return _rightTitleLabel;
}

@end
